/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class WbStatWebAction {

    private String trunk;
    static Logger logger = Logger.getLogger(WbStatWebAction.class.getName());
//新浪微博实时

    public String rt_msgdelay() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String rt_msgdelay = request.getParameter("rt_msgdelay");
        String trunkval = request.getParameter("xl_trunk");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("sina.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("sina.rt.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("sina.head")  + DateUtil.getToDayDate_1() + PicCopy.readProperties("sina.rt.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(PicCopy.readProperties("sina.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("sina.rt.tail"));
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.sina.path") + "1/";
            File fileDir = new File(filePath);
//            logger.info(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
//            logger.info(fs[i]);
        }
        logger.info(fileStrArray);
        out.print(fileStrArray);
        out.close();
        return "success";

    }

    public String rt_tencent() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String rt_msgdelay = request.getParameter("rt_msgdelay");
        String trunkval = request.getParameter("tcwb_turnk");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
        String home_path = PicCopy.readProperties("tenc.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("tenc.rt.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("tenc.head") + DateUtil.getToDayDate_1() + PicCopy.readProperties("tenc.rt.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
        File[] files = file.listFiles();
        String fileStrArray = "";
//        logger.info(System.getProperty("user.dir"));
//        logger.info(files.length);
        for (int i = 0; i < files.length; i++) {
            String filename = files[i].getName();
//            String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\tencimages\\";
            String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.tenc.path") + "1/";
            File fileDir = new File(filePath);
            if (!fileDir.isDirectory()) {
//                logger.info(fileDir.isDirectory());
                fileDir.mkdirs();
//                logger.info(filePath);
            } else {
//                logger.info(fileDir.isDirectory());
//                logger.info(filePath);
            }
            File f = new File(filePath + filename);
            if (f.exists()) {
                //删除原有的
                if (f.delete()) {
                    f.createNewFile();
                }
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            } else {
                f.createNewFile();
//                logger.info("create file");
                PicCopy.copyFile(files[i], f);
//                fileStrArray += filename + ";";
            }
        }
        String[] fs = file.list();
        Arrays.sort(fs);
        for (int i = 0; i < fs.length; i++) {
            fileStrArray += fs[i] + ";";
        }
        out.print(fileStrArray);
        out.close();
        return "success";
    }

    public String sinaDaily() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("rt_msgdelay");
        String trunkval = request.getParameter("xl_trunk");

        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/sina/" + selectedDate + "/24");
        String home_path = PicCopy.readProperties("sina.head") + selectedDate + PicCopy.readProperties("sina.daily.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("sina.head")  + selectedDate + PicCopy.readProperties("sina.daily.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                logger.info(filename);
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.sina.path") + selectedDate + "/24/";
                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            out.print("");
            out.close();
            return "success";
        }
    }

    public String tencDaily() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("rt_msgdelay");

        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/tenc/" + selectedDate + "/24");
        String trunkval = request.getParameter("tcwb_turnk");
        String home_path = PicCopy.readProperties("tenc.head") + selectedDate + PicCopy.readProperties("tenc.daily.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("tenc.head")  + selectedDate + PicCopy.readProperties("tenc.daily.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
//        File file = new File(PicCopy.readProperties("tenc.head") + selectedDate + PicCopy.readProperties("tenc.daily.tail"));

        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
//            logger.info(System.getProperty("user.dir"));
//            logger.info(files.length);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.tenc.path") + selectedDate + "/24/";

                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            out.print("");
            out.close();
            return "success";
        }
    }

    public String tenc_weekly() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("tenc_weekly_selectedDay");
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/tenc/" + selectedDate + "/168");
        String trunkval = request.getParameter("tcwb_turnk");
        String home_path = PicCopy.readProperties("tenc.head") + selectedDate + PicCopy.readProperties("tenc.weekly.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("tenc.head")  + selectedDate + PicCopy.readProperties("tenc.weekly.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
//        File file = new File(PicCopy.readProperties("tenc.head") + selectedDate + PicCopy.readProperties("tenc.weekly.tail"));

        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
//            logger.info(System.getProperty("user.dir"));
//            logger.info(files.length);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
//                String filePath = System.getProperty("user.dir") + "/" + "webapps/ban-ycl-wx-stat-web-1.0/tencimages/" + selectedDate + "/168/";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.tenc.path") + selectedDate + "/168/";

                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            logger.info("there is no this fileDirectory or File");
            out.print("");
            out.close();
            return "success";
        }
    }

    public String sina_weekly() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String selectedDate = request.getParameter("sina_weekly_selectedDay");
        String trunkval = request.getParameter("xl_trunk");
        //根据日期选择图片并
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
//        logger.info(selectedDate);
//        File file = new File("E:\\images");
//        File file = new File("/home/ycl/plot/new/plot_test/pic/sina/" + selectedDate + "/168");
        String home_path = PicCopy.readProperties("sina.head") + selectedDate + PicCopy.readProperties("sina.weekly.tail");
        if (trunkval.equals("yizhuang")) {//默认为数北
            home_path = PicCopy.readProperties("sina.head")  + selectedDate + PicCopy.readProperties("sina.weekly.tail");
        }
        File file = new File(home_path);
        logger.info(home_path);
        if (file.isDirectory() && file.exists()) {

            File[] files = file.listFiles();
            String fileStrArray = "";
//            logger.info(System.getProperty("user.dir"));
//            logger.info(files.length);
            for (int i = 0; i < files.length; i++) {
                String filename = files[i].getName();
//                String filePath = "E:\\ban-ycl-wb-stat-web\\src\\main\\webapp\\images\\";
//                String filePath = System.getProperty("user.dir") + "/" + "webapps/ban-ycl-wx-stat-web-1.0/sinaimages/" + selectedDate + "/168/";
                String filePath = System.getProperty("user.dir") + "/" + PicCopy.readProperties("webapp.sina.path") + selectedDate + "/168/";

                File fileDir = new File(filePath);
                if (!fileDir.isDirectory()) {
//                    logger.info(fileDir.isDirectory());
                    fileDir.mkdirs();
//                    logger.info(filePath);
                } else {
//                    logger.info(fileDir.isDirectory());
//                    logger.info(filePath);
                    //清空原有的文件

                }
                File f = new File(filePath + filename);
                if (f.exists()) {
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                } else {
                    f.createNewFile();
//                logger.info("create file");
                    PicCopy.copyFile(files[i], f);
//                    fileStrArray += filename + ";";
                }
            }
            String[] fs = file.list();
            Arrays.sort(fs);
            for (int i = 0; i < fs.length; i++) {
                fileStrArray += fs[i] + ";";
            }
            out.print(fileStrArray);
            out.close();
            return "success";
        } else {
            logger.info("there is no this fileDirectory or File");
            out.print("");
            out.close();
            return "success";
        }
    }

    public String mainPage() {
        return "success";
    }

    public String wb_sina_rt() {
        return "success";
    }

    public String wb_sina() {
        HttpServletRequest request = ServletActionContext.getRequest();
        trunk = request.getParameter("trunk");
        return "success";
    }

    public String getTrunk() {
        return trunk;
    }

    public void setTrunk(String trunk) {
        this.trunk = trunk;
    }

}
